/*
 * Copyright (C) 2008 Peter Kling
 * 
 * This file is part of 'A Kind of Billiard', a graphical billiard simulation.
 * 
 * 'A Kind of Billiard' is free software: you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * 'A Kind of Billiard' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 'A Kind of Billiard'. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package de.akob.modeling;

import javax.media.opengl.GL;

import de.akob.mathematics.MathVector;
import de.akob.mathematics.Mathematics;

/**
 * Data structure for edges of a model. Note that edges are undirected. Therefore, one should not rely on the order of
 * the edge vertices.
 */
public class Edge {
	private final Vertex[] vertices;
	private MathVector     normal = null;
	
	/**
	 * Constructs an edge for the given vertices.
	 * 
	 * @param vertices vertices for the edge.
	 * @throws IllegalArgumentException if number of given vertices does not match two.
	 */
	public Edge(Vertex... vertices) {
		if (vertices.length != 2)
			throw new IllegalArgumentException("number of vertices for edge must match two");
		this.vertices = vertices;
	}
	
	/**
	 * Draws the edge normal (if present).
	 * 
	 * @param gl OpenGL object used for drawing
	 * @param length length the normal is drawn with
	 */
	public void drawNormal(GL gl, double length) {
		if (normal == null)
			return;
		MathVector meanVector = Mathematics.add(vertices[0], vertices[1]);
		meanVector.mul(0.5);
		gl.glPushMatrix();
		gl.glTranslated(meanVector.get(1), meanVector.get(2), meanVector.get(3));
		gl.glBegin(GL.GL_LINES);
		gl.glVertex3d(0., 0., 0.);
		gl.glVertex3d(normal.get(1) * length, normal.get(2) * length, normal.get(3) * length);
		gl.glEnd();
		gl.glPopMatrix();
	}
	
	/**
	 * Returns the normal of this edge.
	 * 
	 * @return edge normal (null if not present)
	 */
	public MathVector getNormal() {
		return normal;
	}
	
	/**
	 * Executes the OpenGL operations corresponding to this primitive. Needs to be enclosed in proper
	 * <code>glBegin</code> and <code>glEnd</code> calls.
	 * 
	 * @param gl OpenGL object used for drawing
	 * @param useEdgeNormal whether to use the edge normal instead of vertex normals (if present)
	 */
	public void glEvaluate(GL gl, boolean useEdgeNormal) {
		if (normal != null && useEdgeNormal) {
			gl.glNormal3dv(normal.getElements(), 0);
			for (Vertex vertex : vertices)
				vertex.glEvaluate(gl, false);
		} else {
			for (Vertex vertex : vertices)
				vertex.glEvaluate(gl, true);
		}
	}
	
	/**
	 * Sets the normal of this edge. Note that the given vector will be normalized.
	 * 
	 * @param normal the edge normal
	 */
	public void setNormal(MathVector normal) {
		normal.normalize();
		this.normal = normal;
	}
}
